package com.lindroy.androidplayer.ui.main

import android.os.Bundle
import android.os.SystemClock
import androidx.activity.OnBackPressedCallback
import androidx.lifecycle.distinctUntilChanged
import androidx.navigation.findNavController
import com.elvishew.xlog.XLog
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.App
import com.lindroy.androidplayer.base.BaseActivity
import com.lindroy.androidplayer.databinding.ActivityMainBinding
import com.lindroy.androidplayer.ui.main.fragment.MineFragment
import com.lindroy.androidplayer.utils.no
import com.lindroy.androidplayer.utils.primaryColor
import com.lindroy.androidplayer.utils.yes
import com.lindroy.lib_utils.extensions.getResColor
import com.lindroy.lib_utils.extensions.shortToast
import com.lindroy.lib_utils.extensions.statusbar.setStatusBarColor
import com.lindroy.lib_utils.extensions.statusbar.setStatusBarDarkMode
import com.lindroy.lib_utils.extensions.statusbar.setStatusBarLightMode
import com.lindroy.lib_utils.extensions.statusbar.setTransParentStatusBar

class MainActivity : BaseActivity<ActivityMainBinding>() {
    private val mHits = LongArray(2)

    override fun ActivityMainBinding.initView(savedInstanceState: Bundle?) {
    }

    override fun ActivityMainBinding.initListener() {
        onBackPressedDispatcher.addCallback(this@MainActivity,
            object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    findNavController(R.id.nav_main_act).also {
                        if (it.currentDestination != null && it.currentDestination!!.id != R.id.mainFragment) {
                            it.navigateUp()
                        } else {
                            System.arraycopy(mHits, 1, mHits, 0, mHits.size - 1)
                            mHits[mHits.size - 1] = SystemClock.uptimeMillis()//开机后计算的时间
                            if (mHits[0] < (SystemClock.uptimeMillis() - 1500)) { //1500为最后一次点击和第一次点击的间隔时间
                                shortToast(getString(R.string.exit_app))
                            } else {
                                finish()
                            }
                        }
                    }

                }
            })

        App.eventViewModel.isStatusBarDarkMode.observe(this@MainActivity){
            it.yes {
                setStatusBarDarkMode()
            }.no {
                setStatusBarLightMode()
            }
        }
    }
}